<?php

namespace App\Http\Controllers\Api\v2;

use App\Http\Controllers\ApiController;
use App\Models\ItemPush;
use Illuminate\Support\Facades\Cache;

class ItemPushController extends ApiController
{
    public function getItemList(){
        $offset = $this->request->input('offset');
        $count  = $this->request->input('count');
        $offset = $offset ? $offset : 0;
        $count  = $count ? $count : 20;

        $groupid = $this->request->input('groupid');
        $key = 'ItemPush_'.$groupid.'_'.$offset.'_'.$count;
        $items = Cache::remember($key, 5, function () use ($groupid, $offset, $count){
            $items = ItemPush::with('item')->whereHas('item', function ($query){
                return $query->onSale();
            })->where('groupid', $groupid)->offset($offset)->limit($count)
                ->orderByDesc('id')->get()->map(function ($item){
                $item->item->thumb = image_url($item->item->thumb);
                $item->item->image = image_url($item->item->image);
                return $item->item;
            });
            return $items;
        });

        return ajaxReturn(['items'=>$items]);
    }
}
